/**
 * @version test.c v 1.0.0
 * @author Dr.NP <np@bsgroup.org>
 * @package libcomoroclient
 *
 * Library test
 */

/*
 * Compile me with command "gcc -O3 -levent -lcomoroclient test.c -o test"
 * Make sure you has installed the client library
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <event.h>

#ifdef HAVE_MALLOC_H
#ifndef __OpenBSD__
#include <malloc.h>
#endif
#endif

#include "comoro.h"

COMORO comoro;
COMORO_BASE_SRV *base_list;
static int base_id;
static int runner_id;

void get_status(int status)
{
	char *p[3] = {"Hello", "World", "123456"};
	
	if (status == STATUS_PRE_LOGIN)
	{
		comoro_login(&comoro, "user", "pass", runner_id);
	}
	
	else if (status == STATUS_LOGIN_SUCCESS)
	{
		fprintf(stderr, "Login successfully\n");
		comoro_get_base_list(&comoro);
	}
	
	else if (status == STATUS_LOGIN_FAILED)
	{
		fprintf(stderr, "Login failed\n");
	}
	
	else if (status == STATUS_LISTED_SERVER)
	{
		int total = 0;
		int i = 0;
		
		base_list = comoro_base_list(&total);
		fprintf(stderr, "Server list :\n");
		
		for (i = 0; i < total; i ++)
		{
			fprintf(stderr, "Server ID : %u\tAddr : %u\tPort : %u\tNum clients : %u\n", 
				base_list[i].base_id, 
				base_list[i].srv_addr, 
				base_list[i].srv_port, 
				base_list[i].num_clients
			);
		}
		
		// Select last server
		if (total > 0)
		{
			base_id = total - 1;
			fprintf(stderr, "Select server %d\n", base_id);
			comoro_select_server(&comoro, base_id);
		}
	}
	
	else if (status == STATUS_SELECTED_SERVER)
	{
		fprintf(stderr, "Selected base, try to disconnect from login server\n");
		comoro_disconnect(&comoro);
	}
	
	else if (status == STATUS_CLOSED_LOGIN)
	{
		fprintf(stderr, "Disconnected from login server, try to connect to base server %u:%u\n", base_list[base_id].srv_addr, base_list[base_id].srv_port);
		if (RTN_SUCCESS != comoro_connect(&comoro, base_list[base_id].srv_addr, base_list[base_id].srv_port))
		{
			fprintf(stderr, "Cannot connect to base server %u:%u\n", base_list[base_id].srv_addr, base_list[base_id].srv_port);
		}
	}
	
	else if (status == STATUS_PRE_SESSION)
	{
		fprintf(stderr, "Connect successfully, Try to verify session\n");
		comoro_validate_session(&comoro, runner_id);
	}
	
	else if (status == STATUS_SESSION_SUCCESS)
	{
		fprintf(stderr, "Session verified, go to normal mode and sending data\n");
		comoro_psend(&comoro, 200, 3, p);
	}
	
	else if (status == STATUS_SESSION_FAILED)
	{
		fprintf(stderr, "Session failed, disconnect from base\n");
		comoro_disconnect(&comoro);
	}
	
	else
	{
		// What are you doing
	}
	
	return;
}

void get_command(int cmd_id, int plen, char **params)
{
	int i;
	
	fprintf(stderr, "Command ID : %d\n", cmd_id);
	
	for (i = 0; i < plen; i ++)
	{
		fprintf(stderr, "Param %d => %s\n", i, params[i]);
	}
	
	return;
}

int main(int argc, char **argv)
{
	base_id = 0;
	runner_id = 0;
	int port = 65000;
	
	if (argc < 2)
	{
		fprintf(stderr, "Usage : test LOGIN_SERVER ADDRESS [LOGIN_SERVER PORT [APP_ID]]\n");
		
		return RTN_GENERAL;
	}
	
	if (argc > 2)
	{
		port = atoi(argv[2]);
	}
	
	if (argc > 3)
	{
		runner_id = atoi(argv[3]);
	}
	
	if (runner_id < 1)
	{
		runner_id = 1;
	}
	
	comoro_init(&comoro);
	comoro.on_command = get_command;
	comoro.on_status = get_status;
	
	if (RTN_SUCCESS == comoro_connect(&comoro, inet_addr(argv[1]), port))
	{
		event_base_loop(comoro.ev_base, 0);
	}
	
	else
	{
		fprintf(stderr, "Cannot connect to server %s:65000\n", argv[1]);
	}
	
	return RTN_SUCCESS;
}
